<?php
/**
 * @用户Model
 * @author cenchy
 **/
class UserModel extends Model
{
	/**对应数据表名**/
	protected $tableName='user';

	/**需要验证的字段**/
	protected $_validate=array(
		//array(field,rule,message,condition,type,when,params);
	);
	/**自动完成**/
	protected $_auto=array(
		//array('field','填充内容','填充条件','附加规则',[额外参数]);
	);

	/**保存各个用户数据**/
	private static $objects;

	/**获取一条记录对象**/
	public function getObject($userId){
		$key = $this->getKey($userId);
		//内存存在
		if(isset(self::$objects[$key])){
			return self::$objects[$key];
		}
		//缓存存在
		if(S($key)){
			$data = S($key);
			self::$objects[$key] = $data;
			return self::$objects[$key];
		}else{
			$condition['Id'] = $userId;
			$vo = $this->where($condition)->find();
			if($vo){
				S($key,$vo);
				self::$objects[$key] = $vo;
				return $vo;
			}else{
				return false;
			}
		}
		return false;
	}

	/**根据主键更新**/
	public function saveObject($vo){
		$userId = $vo['Id'];
		if(!$userId){
			return false;
		}
		$result = $this->save($vo);
		if($result){
			$key = $this->getKey($vo['Id']);
			$this->clearCache($key);
			return $vo;
		}else{
			return false;
		}
	}

	public function delObject($userId){
		$result = $this->delete($this->Id);
		if(false !== $result){
			$key = $this->getKey($userId);
			$this->clearCache($key);
		}
		return $result;
	}

	private function getKey($userid){
		return $userid;
	}

	private function clearCache($key){
		S($key,null);//删除清缓存
		unset(self::$objects[$key]);//更新清缓存
	}

	public function __destruct(){

	}

}//类定义结束
?>
